package com.pengtu.gsj.service.impl.check.lzgl.basEduca;

import java.util.ArrayList;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.lzgl.LawsDisseNoteDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.LawsDisseNote;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.basEduca.LawsDisseService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class LawsDisseServiceImpl implements LawsDisseService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public LawsDisseNoteDao lawsDisseNoteDao;
	
	@Override
	public Map<String, Object> getLawsDisses(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<LawsDisseNote> lawsLists = new ArrayList<LawsDisseNote>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double tableFull = yearFull;
		double percentage = 0; //完善率
		double total = 0; //计算总分
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		lawsLists = lawsDisseNoteDao.getAll();
		total = lawsLists.size() >= 4 ? yearFull : yearFull - (4 - lawsLists.size()) * yearFull / 3;
		total = total > 0 ? total : 0;
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("count", lawsLists.size());
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
